package com.raymond.cmpp.utils;

import com.google.common.base.Preconditions;

import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Map;

import static java.util.Arrays.copyOf;

/**
 * 公共工具类
 *
 * @author :  raymond
 * @version :  V1.0
 * @date :  2021-08-05 16:28
 */
public class CommonUtil {

    private static final Map<Integer, Charset> ENCODE_MAP = new HashMap<>();

    static {
        ENCODE_MAP.put(0, StandardCharsets.ISO_8859_1);
        ENCODE_MAP.put(8, Charset.forName("UnicodeBigUnmarked"));
        ENCODE_MAP.put(15, Charset.forName("GBK"));
        ENCODE_MAP.put(24, StandardCharsets.UTF_16BE);
    }

    /**
     * 确保长度
     * @param array 字节数组
     * @param minLength 最小长度
     * @param padding 填充长度
     * @return 字节数组
     */
    public static byte[] ensureLength(byte[] array, int minLength, int padding) {
        Preconditions.checkArgument(minLength >= 0, "Invalid minLength: %s", minLength);
        Preconditions.checkArgument(padding >= 0, "Invalid padding: %s", padding);
        if(array.length == minLength)  {
            return array;
        }
        return array.length > minLength ? copyOf(array, minLength) : copyOf(array, minLength + padding);
    }

    /**
     * 确保长度
     * @param array 字节数组
     * @param minLength 最小长度
     * @return 字节数组
     */
    public static byte[] ensureLength(byte[] array, int minLength) {
        Preconditions.checkArgument(minLength >= 0, "Invalid minLength: %s", minLength);
        Preconditions.checkArgument(array.length <= minLength, "Invalid minLength: %s", minLength);
        return array.length < minLength ? copyOf(array, minLength) : array;
    }

    public static void main(String[] args) {
        String s = "";
        byte[] bytes = ensureLength(s.getBytes(), 3);
        for (byte aByte : bytes) {
            System.out.println(aByte);
        }
        System.out.println(bytes.length);
        System.out.println(new String(bytes));
    }

    /**
     * 消息编码
     * @param msgFmt msgFmt
     * @param msgContent 消息内容
     * @return 消息内容字节
     */
    public static byte[] getMsgContentBytes(byte msgFmt, String msgContent) {
        Charset charset = ENCODE_MAP.get((int) msgFmt);
        if (charset != null) {
            return msgContent.getBytes(charset);
        }
        if (msgFmt == 4) {
            return hexStringToByteArray(msgContent);
        }
        return msgContent.getBytes(Charset.forName("GBK"));
    }

    private static byte[] hexStringToByteArray(String content) {
        byte[] b = new byte[content.length() / 2];
        for (int i = 0; i < content.length(); i += 2) {
            b[i / 2] = hexStringToByte(content.substring(i, i + 2));
        }

        return b;
    }

    private static byte hexStringToByte(String content) {
        int i = content.toUpperCase().charAt(0);
        int j = content.toUpperCase().charAt(1);
        if(i > 57) {
            i = (i - 65) + 10;
        } else {
            i -= 48;
        }
        if(j > 57) {
            j = (j - 65) + 10;
        } else {
            j -= 48;
        }
        return (byte) (i * 16 + j);
    }

}
